const api = require('../../api/index');
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
     days: {
       type: Number,
       value: 0
     }
  },
  // 观察数据变化
  observers:{
    days:function (value) {
      if (value) {
          // console.log(value)
          this.data.arr.forEach((item,index,arr)=>{
              if(index < arr.length - 1) {
                if(value>=item.days && value < arr[index+1].days){
                    let leaves = arr[index+1].days - value;
                    this.setData({
                      index,
                      leaves
                    })
                    app.globalData.BG_URL = arr[index].icon;
                    // console.log({index})
                    // console.log(arr[index].src)
                    wx.setStorageSync('BG_URL', arr[index].src)
                  }
              }
          })
          if(value > 100) {
            this.setData({
              isShow: false
            })
          }

          this.getCountFn()
      }
  },
  },
  /**
   * 组件的初始数据
   */
  data: {
      count: 0,
      msg: '当前等级',
      isShow: true,
      index: 0,
      leaves: 0,//还有多少天
      arr: [
        {
          id:1,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/77.png?sign=72ced02e1af8d17d187ef1cb87347b6f&t=1691607139',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/7.png?sign=032363d3fb0992fe416ba3e665c496ae&t=1691607153',
          name: '斗者',
          days: 0,
          text:"坚韧黑铁",
          color: "#D1D0E1"
        },
        {
          id:2,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/22.png?sign=f11fd4e3bec2bc2407d2cf5a83a6b2dd&t=1691607197',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/7.png?sign=52563869496898cfcc090cb09519051b&t=1691607210',
          name: '斗师',
          days: 5,
          text:"倔强青铜",
          color: "#F5CFB1"
        },
        {
          id:3,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/33.png?sign=64cef0a1769e5d2ea804ec4034d2f5ef&t=1691607258',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/3.png?sign=931b1560d5c68dad109fc360481569e6&t=1691607270',
          name: '斗皇',
          days: 20,
          text:"不屈白银",
          color: "#8BCBFE"
        },
        {
          id:4,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/44.png?sign=31a98ffd5833527cfcda77e1615d8b13&t=1691607294',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/4.png?sign=36da172347214147956d978ca3c38d91&t=1691607306',
          name: '斗宗',
          days: 30,
          text:"荣耀黄金",
          color: "#8299D5"
        },
        {
          id:5,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/55.png?sign=9e847786c27ec2afddf035cf4dd5e27d&t=1691607348',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/5.png?sign=c680c164f63679dd41fa068cc2af0c07&t=1691607367',
          name: '斗尊',
          days: 40,
          text:"华贵铂金",
          color: "#D0AEE5"
        },
        {
          id:6,
          src: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/66.png?sign=fd086919dd568d502337a3a7a825ce4f&t=1691607401',
          icon: 'https://7761-wang2023-5gng88vf3d3ea81f-1256272801.tcb.qcloud.la/vips/6.png?sign=8eaff01e6e26fc1c0d3ec441834dafca&t=1691607419',
          name: '斗圣',
          days: 200,
          text:"璀璨钻石",
          color: "#F0EFF5"
        },
      ]
  },
  lifetimes:{
    ready(){
        // console.log("--->",this.days)
        let openid = wx.getStorageSync('openid')
        this.getCountFn(openid)
      }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getCountFn(openid){
      var openid = openid || wx.getStorageSync('openid')
      await api.siginCountFunc(openid).then(
        res =>{
          // console.log(res)
          this.setData({
            count: res.total 
          })
          // let index = this.data.index;
          // app.globalData.BG_URL = this.data.arr[index+1].src;
        }
      )
    },
  }
})
